//
//  LWVisitorView.swift
//  Swift_Weibo_HLW
//
//  Created by huanglinwang on 16/9/20.
//  Copyright © 2016年 Linwang. All rights reserved.
//

import UIKit

protocol LWVisitorViewDelegate: NSObjectProtocol{
    /// 访客视图将要登录
    func visitorLoginViewWillLogin();
    /// 访客视图将要注册
    func visitorLoginViewWillRegister();
}


class LWVisitorView: UIView {

    
    //设置代理对象
    weak var delegate: LWVisitorViewDelegate?
    
    //闭包
    var loginClosure: (()->())?
    //初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    // MARK: - 懒加载控件
    //旋转视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //罩层视图
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //主页
    private lazy var houseImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    

    // 提示 label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 13)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        // 文本对齐方式
        label.textAlignment = .center
        return label
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(registerButtonClick), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.orange, for: .normal)
        
        return button
    }()
    
    // 登录按钮
    lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(loginButtonClick), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        return button
    }()
    

    //使用xib给一个提示,告诉你不能用哈哈哈哈
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(houseImageView)
        addSubview(messageLabel)
        
        
        //布局
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        houseImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }

        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(houseImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 34))
        }
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(loginButton)
        }
        
//        //使用系统自动布局
//        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//        //设置布局方式
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        
        
        //修改访客视图
        
    }
    
    
    func updateVisitorInfo(message: String? ,imageName: String?){
        
        
        
        
        if let msg = message, let imgName = imageName {
            messageLabel.text = msg
            houseImageView.image = UIImage(named: imgName)
            
            cycleImageView.isHidden = true
            
            
        }else{
            
            //代码到此表示首页
            //                执行旋转动画
            cycleImageView.isHidden = false
            startAnim()
            
        }
        
    }
    
    
    //首页的旋转
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 旋转
        anim.toValue = 2 * M_PI
        // 执行时间
        anim.duration = 20
        // 执行次数
        anim.repeatCount = MAXFLOAT
        // 切换界面的时候动画会被释放，指定为false之后切换界面动画就不会被释放
        anim.isRemovedOnCompletion = false
        // 添加动画
        cycleImageView.layer.add(anim, forKey: nil)
    }
    
    
// 按钮点击调用的方法
    @objc private func loginAction(){
    
//        print("我是访客视图")
        
        loginClosure?()
    
    }
    
    
    //注册按钮
    @objc private func registerButtonClick(){
        delegate?.visitorLoginViewWillRegister()
    }
    //登陆按钮
    @objc private func loginButtonClick(){
        delegate?.visitorLoginViewWillLogin()
    }
    
    
}
